package com.harvest.scientific.ladder.page.fa

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.harvest.scientific.ladder.net.ReRsitory
import com.harvest.scientific.ladder.net.request
import com.harvest.scientific.ladder.res.ChiefPeperHumourKidRes
import com.harvest.scientific.ladder.res.MentalDrinkRes
import com.harvest.scientific.ladder.res.QueryProMissRes
import com.harvest.scientific.ladder.util.LogUtils
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.asSharedFlow
import kotlinx.coroutines.launch

/**
 * @author：zenglingwen
 * @date：2024-06-15 14:01
 * @desc：
 */
class AmountViewModel : ViewModel() {

    private val repository by lazy {ReRsitory() }

    private val _queryProMissFlow by lazy { MutableSharedFlow<QueryProMissRes>() }
    val queryProMissFlow by lazy { _queryProMissFlow.asSharedFlow() }

    private val _mentalDrinkRes by lazy { MutableSharedFlow<MentalDrinkRes>() }
    val mentalDrinkRes by lazy { _mentalDrinkRes.asSharedFlow() }

    private val _chiefPepperHumourKid by lazy { MutableSharedFlow<ChiefPeperHumourKidRes>() }
    val chiefPepperHumourKid by lazy { _chiefPepperHumourKid.asSharedFlow() }

    fun queryProMissRes() {
        request(requestBlock = {

            repository.missHungryFirewood()
        }, callBack = {
            onSuccess = { res ->
                res?.let {
                    LogUtils.d("queryPromissres res->>$it")
                    viewModelScope.launch {
                        _queryProMissFlow.emit(it)
                    }
                }
            }
            onFailed = {code: Int?, msg: String? ->

                LogUtils.d("queryProMissRes, error $code, $msg")
            }
        })
    }

    fun mentalDrinkDryerDouble(usefulTailChocolate: Int , thirstyHousewifeBoxTomato: Int , badShoulderLamb: String) {
        request(requestBlock = {
            repository.mentalDrinkDryerDouble(usefulTailChocolate, thirstyHousewifeBoxTomato, badShoulderLamb)
        }, callBack = {
            onSuccess = { res ->
                res?.let {
                    LogUtils.d("mentalDrinkDryerDouble res->>$it")
                    viewModelScope.launch {
                        _mentalDrinkRes.emit(it)
                    }
                }
            }
            onFailed = {code: Int?, msg: String? ->

                LogUtils.d("mentalDrinkDryerDouble, error, $code, $msg")
            }
        })
    }

    fun chiefPepperHumourKid(usefulTailChocolate: Int , thirstyHousewifeBoxTomato: Int , badShoulderLamb: String) {
        request(requestBlock = {
            repository.chiefPepperHumourKid(usefulTailChocolate, thirstyHousewifeBoxTomato, badShoulderLamb)
        }, callBack = {
            onSuccess = { res ->
                res?.let {
                    LogUtils.d("chiefPepperHumourKid res->>$it")
                    viewModelScope.launch {
//                        _mentalDrinkRes.emit(it)
                        _chiefPepperHumourKid.emit(it)
                    }
                }
            }
            onFailed = {code: Int?, msg: String? ->
                LogUtils.d("chiefPepperHumourKid,error $code, $msg")
                viewModelScope.launch {
                    _chiefPepperHumourKid.emit(ChiefPeperHumourKidRes(customData = msg ?: ""))
                }
            }
        })
    }
}